package com.hehe.boot.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hehe.boot.mapper.ClinicMapper;
import com.hehe.boot.pojo.Clinic;
import com.hehe.boot.pojo.ClinicHttp.ClinicRegisterRequest;
import com.hehe.boot.pojo.ClinicHttp.ClinicShowDetailResponse;
import com.hehe.boot.pojo.Doctor;
import com.hehe.boot.pojo.Drug;
import com.hehe.boot.pojo.User;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ClinicService extends ServiceImpl<ClinicMapper, Clinic> {

    private final DoctorService doctorService;

    public ClinicService(DoctorService doctorService) {
        this.doctorService = doctorService;
    }

    public Long register (ClinicRegisterRequest request){
        Clinic clinic = new Clinic();
        clinic.setName(request.getName());
        clinic.setDetail(request.getDetail());
        clinic.setAddress(request.getAddress());
        this.save(clinic);
        return clinic.getId();
    }

    @Override
    public List<Clinic> list() {
        return super.list();
    }

    @Transactional
    public ClinicShowDetailResponse showDetailByClinicId(Integer clinicId) {
        ClinicShowDetailResponse clinicShowDetailResponse = new ClinicShowDetailResponse();
        clinicShowDetailResponse.setClinic(this.getById(clinicId));

        QueryWrapper<Doctor> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("clinic_id", clinicId);

        List<Doctor> list = this.doctorService.list(queryWrapper);

        clinicShowDetailResponse.setDoctors(list);

        return clinicShowDetailResponse;
    }

    public List<Clinic> like(String name) {
        QueryWrapper<Clinic> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", name);

        return this.list(queryWrapper);
    }

    public IPage<Clinic> getProPage(int currentPage){
        // 构造分页条件
        Page<Clinic> page = new Page<>(currentPage, 10);

        // 执行分页查询
        return this.page(page);
    }

    public IPage<Clinic> getProPageLike(String query,int currentPage) {
        // 构造分页条件
        Page<Clinic> page = new Page<>(currentPage, 10);

        // 构造查询条件
        QueryWrapper<Clinic> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", query);

        // 执行分页查询
        return page(page, queryWrapper);
    }

    public Boolean insertDrug(Clinic drug) {
        drug.setId(0L);
        return this.save(drug);
    }

    public Boolean updateDrug(Clinic drug) {
        boolean result = this.updateById(drug);
        return result;
    }

    public Boolean deleteDrug(Clinic drug) {
        boolean result = this.removeById(drug.getId());
        return result;
    }
}
